package com.smedi.ismedi.notification.core.port.adapter.persistence.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smedi.ismedi.notification.core.domain.aggregatemodel.SmsTemplate;
import com.smedi.ismedi.notification.core.domain.repository.SmsTemplateRepository;
import com.smedi.ismedi.notification.core.port.adapter.persistence.model.SmsTemplateEntity;
import com.smedi.ismedi.notification.core.port.adapter.persistence.mybatis.mapper.SmsTemplateMapper;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
@AllArgsConstructor
public class SmsTemplateRepositoryImpl extends ServiceImpl<SmsTemplateMapper, SmsTemplateEntity> implements SmsTemplateRepository, IService<SmsTemplateEntity> {
    private final DozerUtil dozerUtil;

    @Override
    public SmsTemplate selectBySmsTemplateId(Long smsTemplateId) {
        SmsTemplateEntity row = this.getBaseMapper().selectById(smsTemplateId);
        return dozerUtil.convertor(row, SmsTemplate.class);
    }

    @Override
    public SmsTemplate selectBySmsTemplateName(String smsTemplateName) {
        LambdaQueryWrapper<SmsTemplateEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SmsTemplateEntity::getTemplateName,smsTemplateName);
        SmsTemplateEntity smsTemplateEntity = this.baseMapper.selectOne(lambdaQueryWrapper);
        return dozerUtil.convertor(smsTemplateEntity, SmsTemplate.class);
    }

    @Override
    public SmsTemplate doInsert(SmsTemplate data) {
        SmsTemplateEntity smsTemplateEntity = dozerUtil.convertor(data, SmsTemplateEntity.class);
        this.getBaseMapper().insert(smsTemplateEntity);
        return dozerUtil.convertor(smsTemplateEntity, SmsTemplate.class);
    }

    @Override
    public SmsTemplate doUpdate(SmsTemplate date) {
        SmsTemplateEntity smsTemplateEntity = dozerUtil.convertor(date, SmsTemplateEntity.class);
        this.getBaseMapper().updateById(smsTemplateEntity);
        return dozerUtil.convertor(smsTemplateEntity, SmsTemplate.class);
    }

    @Override
    public void doDelete(Long smsTemplateId) {
        this.getBaseMapper().deleteById(smsTemplateId);
    }

    @Override
    public List<SmsTemplate> selectBySmsTemplateStatus(String smsTemplateStatus) {
        QueryWrapper<SmsTemplateEntity> wrapper = new QueryWrapper();
        wrapper.eq("status", smsTemplateStatus);
        List<SmsTemplateEntity> rows = this.getBaseMapper().selectList(wrapper);

        return dozerUtil.convertor(rows, SmsTemplate.class);
    }
}